/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.unicam.fishpass.prism;

//@author Baiocco Andrea

/**
 * it calculates the arithmetic expression that are in the project
 * the case listed are : + , - , * , / , % . 
 */
public class ArithmeticExpression {

    private ArithmeticExpression left = null;
    private ArithmeticExpression right = null;
    private char operand;
    private int num;

    /**
     * @param left
     * @param right
     * @param operand
     * expresses the arithmetic Expression with the operand and left and right part
     */

    
    public ArithmeticExpression(ArithmeticExpression left, ArithmeticExpression right, char operand) {
        this.left = left;
        this.right = right;
        this.operand = operand;
    }

    /**
     * @param num
     */
    public ArithmeticExpression(int num) {
        this.num = num;
    }

    int calculateExpression() {
        if (right == null) {
            return num;
        }
        int valueLeft = left.calculateExpression();
        int valueRight = right.calculateExpression();
        switch (operand) {
            case ('+'):
                return valueLeft + valueRight;
            case ('-'):
                return valueLeft - valueRight;
            case ('*'):
                return valueLeft * valueRight;
            case ('/'):
                return valueLeft / valueRight;
            case ('%'):
                return valueLeft % valueRight;
        }
        return 0;
    }

    @Override
    public String toString() {
        if (left == null) {
            return Integer.toString(num);
        }
        return "(" + left.toString() + operand + right.toString() + ")";
    }
}